package com.scud.system.model;

import com.alibaba.fastjson.JSON;
import com.scud.model.system.pojo.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails {

    private User user;

    private List<String> permissionList;
    public LoginUser(User user,List<String> permissionList){
        this.user=user;
        this.permissionList=permissionList;
    }

    //权限集合
    List<SimpleGrantedAuthority> authorities;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities!=null){
            return authorities;
        }
        authorities=new ArrayList<>();
        for (String item : permissionList) {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(item);
            authorities.add(simpleGrantedAuthority);
        }
        return authorities;
    }

    //密码
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    //用户名
    @Override
    public String getUsername() {
        return JSON.toJSONString(user);
    }

    //账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账号是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    //账号是否没有超时
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //账号是否可用
    @Override
    public boolean isEnabled() {
        return true;
    }
}